
/*
	this
*/


url = goloble_url
//console.log(url);

var app = new Vue({
	el: '#app',
	data: {
		//图片弹窗标识
		popup: 0,
		//获取的用户列表，用于显示在前端
		userList: [],
		modal2: false,
		modal_loading: false,
		userId: '',

		userStatus: [{
				value: '已认证',
				label: '已认证'
			},
			{
				value: '未认证',
				label: '未认证'
			},
		],
		selectStatus: [],
		statusUserId: '',
		UserCount: 0,

		//接收筛选条件
		CanditateDate: '',
		CanditateState: 0,
		inputWorkNum: '',

		img_path: '',


		// -------
		carList: [],
		init_page: 1,
		//url: 'http://114.215.83.227:809/admin/',
		workList: [],
		nameList: [],
		//按时间筛选
		beginDate: '',
		endDate: '',
		beginTime: '',
		endTime: '',
		//buttonState: false,
		carState: '',





	},
	methods: {
		//时间戳转化
		timestampToTime(cjsj) {
			var date = new Date(date);
			var date = new Date(cjsj) //时间戳为10位需*1000，时间戳为13位的话不需乘1000
			var Y = date.getFullYear() + '-'
			var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
			var D = date.getDate() + ' '
			var h = date.getHours() + ':'
			var m = date.getMinutes() + ':'
			var s = date.getSeconds()
			return Y + M + D + h + m + s
			//console.log(timestampToTime(1533293827000))
		},
		//获取筛选条件
		getCanditate() {
			var vm = this;
			//console.log("getCanditate：" + this.CanditateDate)
			if (vm.CanditateDate == "未认证") {

				vm.CanditateState = 1

			} else if (vm.CanditateDate == "已认证") {

				vm.CanditateState = 2
				//console.log("auditState2："+auditState)
			}
			//console.log("getCanditate1：" + vm.CanditateState)


		},
		//转化成标准时间格式
		transformTime(date) {
			var date = new Date(date);
			var y = date.getFullYear();
			var m = date.getMonth() + 1;
			m = m < 10 ? '0' + m : m;
			var d = date.getDate();
			d = d < 10 ? ('0' + d) : d;
			return y + '-' + m + '-' + d;
		},

		//获取页码 并刷新数据  async,await
		backPageNum(page) {
			var vm = this;
			//vm.workList.length = 0;
			// vm.carList.length = 0;
			//console.log("--------------:" + urls );


			if (vm.beginDate.length == 0 || vm.endDate.length == 0 || vm.beginTime.length == 0 || vm.endTime
				.length == 0) {
					//get now time
					var timestamp = Date.parse(new Date());
					//console.log(timestamp)
					var now_time = vm.timestampToTime(timestamp)
					//console.log(now_time)
					var AllBeginDate = '2019-01-01 00:00:00'
					var AllEndDate = now_time
			} else {
				vm.beginDate = this.transformTime(vm.beginDate)
				vm.endDate = this.transformTime(vm.endDate)
				var AllBeginDate = vm.beginDate + " " + vm.beginTime
				var AllEndDate = vm.endDate + " " + vm.endTime
				console.log('AllBeginDate：' + AllBeginDate)
			}





			if (page > 1) {
				vm.init_page = page

			}
			
			// //管理员通过起止时间、审核状态获取时间段内新增车辆列表
			// 必填:
			// 选填：currentPageNo(不填默认为第一页，第一页为1)、carState（不填获取到的就是全部）、beginTime、endTime（气质时间其中有一个为空则获取的为全部，格式：yyyy-MM-dd HH:mm:ss；如：2020-01-01 00:00:00、2020-01-01 15:00:00）
			// http://192.168.3.2:809/admin/adminGetNewAddCarListByBeginTimeEndTime

			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token 2019-01-01 00:00:00 now_time
			this.$http.post(url + 'adminGetNewAddCarListByBeginTimeEndTime', {
				'currentPageNo': vm.init_page,
				'beginTime': AllBeginDate,
				'endTime': AllEndDate,
				'carState': vm.CanditateState
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header,

				},



			}).then(function(res) {
				if (res.data.status == 1) {

					vm.$Message.success('刷新成功')
					//console.log('获取到的是:' + res.data.message[0].carState)


					vm.carList = res.data.message
					//console.log('111获取到的是:' + vm.carList[0].carAfterImg)
					for (var i = 0; i < vm.carList.length; i++) {
						// 判断认证状态
						switch (vm.carList[i].carState) {
							case 1:
								vm.carList[i].carState = "未认证"
								break;
							case 2:
								vm.carList[i].carState = "已认证"
								break;
							default:
								break;
						}
						//时间戳转化
						vm.carList[i].addTime = vm.timestampToTime(vm.carList[i].addTime);

						//获取工号 姓名
						// vm.tranformNameAndNum(vm.carList[i].userId,i);
						// console.log(i+'listuserid:' + vm.carList[i].userId)


					}
				} else if (res.data.status == 0) {

					vm.$Message.error('登录失效，3秒后强制登录....')


					setTimeout(() => {

						window.top.location = "login.html"
					}, 3000)

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});


		},

		// // 通过起止时间、审核状态获取时间段内新增车辆列表
		ByDateCarList() {
			var vm = this;
			if (vm.beginDate.length == 0 || vm.endDate.length == 0 || vm.beginTime.length == 0 || vm.endTime
				.length == 0) {
				vm.$Message.error('请填写必选项')
			} else {
				vm.backPageNum()
			}


		},

		//"carAfterImg": "http://114.215.83.227:809/userImg/2018250317/carImg/f07d82a4-7da3-41fe-b0e0-ba693161381d_lunbo1.jpeg",
		// "carBeforeImg": "http://114.215.83.227:809/userImg/2018250317/carImg/b76594e6-3bd9-4a79-9e14-b82fbdff3b1b_f1bf26088d0db8a5eefa280d9bb93c0e.jpg",
		//打开图片弹窗 前车图
		showpopup(carBeforeImg) {
			var vm = this;
			vm.loading();
			//console.log(carBeforeImg)
			vm.img_path = '';
			vm.getimg(carBeforeImg);

			setTimeout(() => {

				this.popup = 1;
				//console.log(hiddenTroubleId)

			}, 1000);


		},
		//打开图片弹窗  后车图
		showpopup2(carAfterImg) {
			var vm = this;
			vm.loading();
			//console.log(carAfterImg)
			vm.img_path = '';
			vm.getimg(carAfterImg);

			setTimeout(() => {

				this.popup = 1;
				//console.log(hiddenTroubleId)

			}, 1000);


		},

		//加载中
		loading() {
			const msg = this.$Message.loading({
				content: 'Loading...',
				duration: 0
			});
			setTimeout(msg, 1000);
		},

		//关闭图片弹窗
		closepopup() {
			this.popup = 0;
		},


		//获取图片 返回一个blob类型的字符串
		getimg(url) {
			var vm = this;
			// url =
			// 	'http://114.215.83.227:809/userImg/2018250027/workImg/2bfac844-df59-4496-a7af-e2ab3b9f120c_2000 (3).jpg'
			var blob_url = '';
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			axios({
				method: 'get',
				url: url, // 请求地址
				headers: {
					'access_header': access_header
				},
				responseType: 'blob' // 设置接收格式为blob格式
			}).then(res => {
				// console.log(res)
				if (res && res.data && res.data.size) {
					const dataInfo = res.data
					let reader = new window.FileReader()
					// 使用readAsArrayBuffer读取文件, result属性中将包含一个 ArrayBuffer 对象以表示所读取文件的数据
					reader.readAsArrayBuffer(dataInfo)
					reader.onload = function(e) {
						const result = e.target.result
						const contentType = dataInfo.type
						// 生成blob图片,需要参数(字节数组, 文件类型)
						const blob = new Blob([result], {
							type: contentType
						})
						// 使用 Blob 创建一个指向类型化数组的URL, URL.createObjectURL是new Blob文件的方法,可以生成一个普通的url,可以直接使用,比如用在img.src上
						const url = window.URL.createObjectURL(blob)
						vm.img_path = url
						// console.log(
						// 	vm.img_path) // 产生一个类似 blob:d3958f5c-0777-0845-9dcf-2cb28783acaf 这样的URL字符串


					}
				}
			});
		},

		// userId获取用户的姓名 工号  async,await
		tranformNameAndNum(userId, index) {
			var vm = this;
			console.log('userId:' + userId)
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(url + 'adminGetUserByUid', {
				'userId': userId,
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header,
				},
			}).then(function(res) {
				if (res.data.status == 1) {
					for (var i = 0; i < index + 1; i++) {
						vm.workList.push('失败');
						vm.nameList.push('失败');
					}
					//console.log('res.data.message.workNum:' + res.data.message.workNum)
					// vm.workListuserid[index] = res.data.message.workNum
					//vm.workList.push(res.data.message)
					vm.workList[index] = res.data.message.workNum
					vm.nameList[index] = res.data.message.name
				} else if (res.data.status == 0) {

					//vm.$Message.error('登录失效，3秒后强制登录....')


					// setTimeout(() => {

					// 	window.top.location = "login.html"
					// }, 3000)

				} else {
					vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				vm.$Message.error('不可预知错误,联系管理员')
			});
		},
		//获取总的用户数量
		getUserSum() {
			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(url + 'adminGetUserCount', {
				// 'currentPageNo': 1
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}


			}).then(function(res) {
				if (res.data.status == 1) {

					//vm.$Message.success('刷新成功999')
					//console.log('获取到的是9999:' + res.data.message.countIsState)
					// vm.userList = res.data.message

					vm.UserCount = res.data.message.countIsState




				} else if (res.data.status == 0) {

					//vm.$Message.error('获取失败，请重新登录')

				} else {
					//vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				//vm.$Message.error('不可预知错误,联系管理员')
			});
		},




		//获取id用于修改状态
		getUserCarId(id) {
			this.statusUserId = id
			console.log("id：" + this.statusUserId)
		},

		// 修改车辆认证状态
		changeStatus(index) {
			var vm = this
			// console.log("changeStatusindex："+index)
			// console.log("111111statusUserId："+vm.statusUserId)
			//    console.log("changeStatusindex："+vm.selectStatus[index])


			if (vm.selectStatus[index] == "未认证") {

				var carState = 1

			} else if (vm.selectStatus[index] == "已认证") {

				var carState = 2
				//console.log("auditState2："+auditState)
			}

			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			// setTimeout(() => {
			this.$http.post(url + 'adminChangeUserCarStateByUserCarId', {
				'userCarId': vm.statusUserId,
				'carState': carState
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {
					//vm.getUserList()
					console.log('111获取到的是:' + res.data.prompt)

					this.$Message.success('修改用户认证状态成功');



				} else if (res.data.status == 0) {

					vm.$Message.error('失败')

					//console.log('111获取到的是:' + res.data.message)

				} else {
					//vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				//vm.$Message.error('不可预知错误,联系管理员')
			});
			// }, 5000);
		},




		//管理员通过车牌号获取该车辆信息列表
		// 必填:carNum  = WorkNum
		// 选填：
		// http://192.168.3.2:809/admin/adminGetCarListByCarNum
		searchForWorkNum() {
			var vm = this;
			// console.log("changeStatusindex："+index)
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token

			if (vm.inputWorkNum.length == 0) {
				vm.$Message.error('车牌不能为空')
			} else {


				this.$http.post(url + 'adminGetCarListByCarNum', {
					'carNum': vm.inputWorkNum
				}, {
					emulateJSON: true,
					headers: {
						'access_header': access_header
					}
				}).then(function(res) {
					if (res.data.status == 1) {


						if (res.data.message == 0) {
							vm.$Message.error('车牌不合法或者前面有空格')
						}
						//console.log(res)
						//console.log('获取到的是searchForWorkNum:' + res.data.message.carNum)
						vm.carList.length = 0;
						vm.carList = res.data.message
						//console.log('111获取到的是:' + vm.carList[0].carAfterImg)

						for (var i = 0; i < vm.carList.length; i++) {
							// 判断认证状态
							switch (vm.carList[i].carState) {
								case 1:
									vm.carList[i].carState = "未认证"
									break;
								case 2:
									vm.carList[i].carState = "已认证"
									break;
								default:
									break;
							}
							//时间戳转化
							vm.carList[i].addTime = vm.timestampToTime(vm.carList[i].addTime);
						}

					} else if (res.data.status == 0) {

						vm.$Message.error('登录失效，3秒后强制登录....')


						// setTimeout(() => {

						// 	window.top.location = "login.html"
						// }, 3000)

					} else {
						vm.$Message.error('不可预知错误')
					}

				}, function(res) {
					console.log('res:' + res.data.status)
					if (res.data.status == 500 || res.data.status == 400) {

						vm.$Message.error('请输入正确工号....')

					} else {
						vm.$Message.error('未知错误....')
					}




				});


			}






		},
		//获取id用于删除
		userDel(id) {
			console.log('enter:' + id)
			this.userId = id;
		},

		//执行删除用户
		del() {

			this.modal_loading = true;
			setTimeout(() => {
				this.modal_loading = false;
				this.modal2 = false;
				//this.$Message.success('删除成功');
			}, 1000);
			// console.log('获取到的是:' + id)

			var vm = this;
			var access_header = 'a' + sessionStorage.getItem("token"); //获取数据token
			this.$http.post(url + 'adminDelUserByUserId', {
				'userId': vm.userId,
			}, {
				emulateJSON: true,
				headers: {
					'access_header': access_header
				}
			}).then(function(res) {
				if (res.data.status == 1) {
					vm.backPageNum();
					console.log('111获取到的是:' + vm.userList[i].userRole)


				} else if (res.data.status == 0) {

					vm.$Message.error('删除失败')

				} else {
					//vm.$Message.error('不可预知错误')
				}

			}, function(res) {
				//vm.$Message.error('不可预知错误,联系管理员')
			});

		},
		//根据userid查询用户电话号码   userIdPhone =  userid 
		ByUserIdTel(userIdPhone, name) {
			sessionStorage.removeItem("userIdPhone"); //删除数据
			var vm = this;
			//console.log('start:' + userIdPhone)
			// var params={
			// 	'userId':userIdPhone,
			// 	'name':name,

			// }
			// console.log('params:'+params.userId)


			sessionStorage.setItem("userIdPhone", userIdPhone); //存储数据
			sessionStorage.setItem("name", name); //存储数据
		},



	},
	mounted() {
		this.backPageNum()
		//this.getUserSum()
		// this.timestampToTime()

		//this.getimg()
		//this.ByUserIdTel()


	},
	created() {


	},
	watch: {

	},
	directives: {
		trigger: {
			inserted(el, binging) {
				el.click()
				//$(el).trigger('click')
			}
		}
	},


});
